/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Order;

/**
 *
 * @author Wei Liu
 */
public class OrderDAO extends BaseEntityDAO
{

    public OrderDAO()
    {
        // initialization
    }

    /**
     * Insert order into database
     * @param order the new order entity to be inserted
     * @return newly created order number or a 0 on error
     */
    public int insertOrder(Order order)
    {
        return save(order);
    }

    /**
     * Delete order that matches the supplied criteria
     * @param order the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteOrder(Order order)
    {
        return delete(order);
    }

    /**
     * Update the order using data from the order Data Object
     * @param order the order Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateOrder(Order order)
    {
        if (order.getOrderID() == 0)
        {
            return false;
        }

        if (update(order))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the order that matches the supplied criteria
     * @param order the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Order findOrder(Order order)
    {
        String sql = null;

        if (order.getOrderID() != 0)
        {
            sql = buildQuerySQL(order);
        }
        else
        {
            return null;
        }

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(OrderDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Order) this.getObject(crs, order);
    }

    /**
     * Get all orders that match the supplied criteria
     * @param order supplied criteria
     * @return a RowSet
     */
    public RowSet getOrdersRS(Order order)
    {
        String sql = buildQuerySQL(order);
        return query(sql);
    }

    /**
     * Get all orders that match the supplied criteria
     * @param order supplied criteria
     * @return a collection of Order Data Objects
     */
    public Collection<Order> getOrdersDO(Order order)
    {
        String sql = buildQuerySQL(order);

        RowSet crs = query(sql);
        Collection<Order> orders = new ArrayList<Order>();
        try
        {
            while (crs.next())
            {
                Order orderObject = (Order) this.getObject(crs, order);
                Order orderDO = new Order();
                orderDO.setCost(orderObject.getCost());
                orderDO.setCustomerID(orderObject.getCustomerID());
                orderDO.setDueDate(orderObject.getDueDate());
                orderDO.setEventID(orderObject.getEventID());
                orderDO.setOrderDate(orderObject.getOrderDate());
                orderDO.setOrderID(orderObject.getOrderID());
                orderDO.setOrderType(orderObject.getOrderType());
                orderDO.setPaymentDate(orderObject.getPaymentDate());
                orderDO.setShippingMethodID(orderObject.getShippingMethodID());
                orders.add(orderDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(OrderDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return orders;
    }
}
